﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowlegeAssistant
{
    public partial class UsersForm : Form
    {

        private DataTable usertable = null;

        public UsersForm()
        {
            InitializeComponent();
            RefreshData();
        }

        public void RefreshData()
        {
            this.usertable = UserUtil.GetAllUsers(false); 
            this.userstable.RowHeadersVisible = false;
            this.userstable.AutoGenerateColumns = false;
            this.userstable.DataSource = usertable;
            this.userstable.Columns["username"].DataPropertyName = "user_name";
            this.userstable.Columns["role"].DataPropertyName = "role_name";
            this.userstable.Columns["name"].DataPropertyName = "name";
            this.userstable.Columns["owner"].DataPropertyName = "owner_type";
        }

        private void InitTableFields()
        {
           
        }

        private void UsersForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            EditUserForm euf = new EditUserForm();
            euf.ShowDialog();
            RefreshData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (userstable.SelectedRows.Count > 0)
            {
                EditUserForm euf = new EditUserForm(
                    (int)((DataTable)userstable.DataSource).Rows[userstable.SelectedRows[0].Index]["user_id"]
                    );
                euf.ShowDialog();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(userstable.SelectedRows.Count != 0)
            {
                int index = userstable.SelectedRows[0].Index;
                int user_id = int.Parse(this.usertable.Rows[index]["user_id"].ToString());
                UserUtil.DeleteUser(user_id);
                this.userstable.Rows.RemoveAt(index);
               
            }
        }
    }
}
